<?php

namespace App\Http\Controllers\Api;

use App\EcmsHtfComment;
use App\Http\Extend\Helper;
use App\Jobs\SendMsg;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class SendMessageController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function import(Request $request)
    {
        set_time_limit(0);
        $filePath = 'public/kehu.xls';
        Excel::load($filePath, function($reader) {
            $key = 'take';
            Cache::has($key) ? '' : Cache::forever($key,'1'); //添加一个永久缓存，默认发送第一页的数据，检测不存在添加缓存，存在不进行操作
            while (Cache::get($key) <= 10)
            {
                Cache::increment($key,1); //参数自增1
                $take = 100; //一次发送100条
                $skip = (Cache::get($key) - 1)*$take;
                $data = $reader->skip($skip)->take($take)->get()->toArray();
                $telStr = implode(',',array_column($data,'tel'));
                $this->send($telStr);
                sleep(1*60);
            }
        });
    }

    /**
     * 发送通知
     * @param $telStr
     * @return bool
     * create by wenQing
     */
    public function send($telStr)
    {
        return Helper::sendTongZhi($telStr);
    }

    public function send2(Request $request)
    {
        set_time_limit(0);
        $filePath = 'public/kehu.xls';
        Excel::load($filePath, function($reader) {
            Cache::forever('take2','1'); //添加一个永久缓存，默认发送第一页的数据
            while (Cache::get('take2') <= 105)
            {
                Cache::increment('take2',1); //参数自增1
                // 1 201 301 401 每分钟100
                $take = 100; //一次发送100条
                $skip = (Cache::get('take2') - 1)*$take;
                $data = $reader->skip($skip)->take($take)->get()->toArray();
                $telStr = implode(',',array_column($data,'tel'));
                $telStr = '18511462324';
                $this->send($telStr);
                sleep(1*60);
            }
        });
    }

    public function test()
    {
        $key = 'take_test';
        Cache::forever($key,'1'); //添加一个永久缓存，默认发送第一页的数据
        while (Cache::get($key) <= 105)
        {
            $job = (new SendMsg())->delay(Carbon::now()->addMinutes(Cache::get($key)));
            $this->dispatch($job);
            Cache::increment($key,1); //参数自增1
        }
    }

    //Excel文件导出功能 By Laravel学院
    public function export()
    {
        $cellData = [
            ['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        Excel::create('学生成绩',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
}
